package homework;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/abc")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接受前端请求
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");

        //封装对象
        User user = new User();
        user.setId(3);
        user.setName(username);
        user.setPwd(pwd);

        UserDao userDao = new UserDao();
        try {
            userDao.insert(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //响应
        //resp.getWriter().write("添加成功！你好我是老妖");

        req.setAttribute("xxx",username);

        req.getRequestDispatcher("/index.jsp").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
